/***************************************************************************
 * 
 * Copyright (c) 2016 shaxiaoseng.com, Inc. All Rights Reserved
 * $Id$ 
 * 
 **************************************************************************/

/**
 * @file /Stlye/js/commono_operations.js
 * @author kevin(kevin@shaxiaoseng.com)
 * @date 2016/07/21 22:40:06
 * @version $Revision$ 
 * @brief 
 *     Common operations.
 **/

var sxsOpr = (function (window) {
    var layerConf = {
        shadeRate: 0.8
    };
    
    var boxShow = false;
    /**
     * @Author: robin
     * @Date: 2016/07/21 21:54:01
     * @Brief: checkBox批量操作.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] url   处理链接
     * @Prerequisite: 
     * @Return: 
     */  
    function checkBoxAll(target,url,title){
    	$(target).blur();
    	var str = ""; 
    	$("input:checked").each(function(){
    		str+=$(this).val()+",";
    	});
    	str = str.substring(0,str.length-1);
    	if (title==null || title=="") {
    		title = "请确认您的操作!!!!!";
        }
        layer.confirm(title, {shade: layerConf.shadeRate,
        shadeClose: false,
        yes: function () {
        	ajaxRequest(target, url, 'get', str, null);
        },
        btn2: function () {
                  layer.msg("操作取消!");
              },
        cancel: function () {
           
                }
        });	

    } 

    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/21 22:28:29
     * @Brief: 在当前窗口打开另一个页面.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] url  跳转到的url
     * @Prerequisite: 
     * @Return: 
     */
    function butClick(target, url) {
    	$(target).blur();
        if(url.indexOf("?")>0 && url.indexOf("cid")<0){ 
        	self.location.href = url+'&cid='+GetQueryString("cid"); 
        }else{
        	self.location.href = url+'?cid='+GetQueryString("cid"); 
        } 
    }

    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/21 21:45:22
     * @Brief: 弹出一个窗口,并显示一个页面.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  弹出框的标题
     * @Param[in] url  跳转到的url
     * @Prerequisite: 
     * @Return: 
     */
    function popUpWindow(target, title, url, data) {
    	$(target).blur();
    	var urls ="";
        if(data!=null && data!=""){
        	urls= url+"&data="+data+"&id="+data;
        }else{
        	urls= url;	
        }
        layer.open({
            type: 2,
            title: title,
            shadeClose: false,
            shade: layerConf.shadeRate,
            area: ['80%', '90%'],
            fix: true, //固定
            content: urls //iframe的url
        });
        $(target).blur();
    }
 /*
  * 线上财务人工还款 专用
  */
    function popUpMinWindow(target, title, url, data) {
    	$(target).blur();
    	var urls ="";
        if(data!=null && data!=""){
        	if(url.indexOf("?") >= 0){
        		urls= url+"&data="+data;
        	}else{
        		urls= url+"?data="+data;
        	}
        }else{
        	urls= url;	
        }
        layer.open({
            type: 2,
            title: title,
            fix: true, //固定
            shadeClose: false,
            shade: layerConf.shadeRate,
            area: ['480px', '400px'],
            content: urls //iframe的url
        });
    }    
    /**
     * @Author: robin
     * @Date: 2016/07/21 21:54:01
     * @Brief: 弹出一个选择性窗口.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  弹出框的标题
     * @Param[in] txtA  第一个按钮的文字
     * @Param[in] txtB  第二个按钮的文字
     * @Param[in] urlA  第一个按钮的链接
     * @Param[in] urlB  第二个按钮的链接
     * @Prerequisite: 
     * @Return: 
     */
    function popChoose(target, title, txtA, txtB, urlA, urlB) {    
	    layer.confirm(title, {
	      btn: [txtA,txtB] //按钮
	    }, function(){
	    	self.location.href = urlA;
	    }, function(){
	    	self.location.href = urlB;
	    });
    }
    
    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/21 21:54:01
     * @Brief: 弹出一个确定窗口.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  弹出框的标题
     * @Param[in] url  确定后要请求的接口
     * @Param[in] method  请求接口的方法, 如get, post
     * @Param[in] data  请求接口的post数据
     * @Param[in] checkResult  校验响应数据的回调函数, 带一个result参数
     * @Prerequisite: 
     * @Return: 
     */
    function popUpConfirm(target, title, url, method, data, checkResult) {
    	$(target).attr("disabled",true);
    	var daa=$(target).parent().parent().attr("data");
    	var td= $("#TRS"+daa).find("td").eq(2).text();
    	title = title+"<br> 贷款ID号："+td;
        layer.confirm(title, {
            shade: layerConf.shadeRate,
        shadeClose: false,
        yes: function () {
                ajaxRequest(target, url, method, data, checkResult);
        },
        btn2: function () {
                  layer.msg("您取消了操作!");
                  $(target).attr("disabled",false);
              },
        cancel: function () {
                    layer.msg("您取消了操作!");
                    $(target).attr("disabled",false);
                }
        });
    }    

    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/21 21:54:01
     * @Brief: 弹出一个确定窗口.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  弹出框的标题
     * @Param[in] url  确定后要请求的接口
     * @Param[in] method  请求接口的方法, 如get, post
     * @Param[in] data  请求接口的post数据
     * @Param[in] checkResult  校验响应数据的回调函数, 带一个result参数
     * @Prerequisite: 
     * @Return: 
     */
    function popUpMatchingConfirm(target, title, url, method, data, checkResult) {
    	if($(target).val() != "配资" && $(target).val() != "配资失败" && $(target).val() != "已配资"){
    	   $(target).attr("disabled","disabled");	
    	   alert("该贷款配资正在进行中，请稍后查看结果！");
    	   return false;
    	}
    	if($(target).val() == "已配资"){
     	   $(target).attr("disabled","disabled");	
     	   alert("该贷款配资已完成！");
     	   return false;
     	}
        layer.confirm(title, {
            shade: layerConf.shadeRate,
            shadeClose: false,
            yes: function () {
	        	if($(target).val()==="配资"){
	        		ajaxMatching(target, url, method, data, checkResult);
	        	}else{
	        		if($(target).val()==="配资失败"){
	        			ajaxMatching(target, url, method, data, checkResult);
	        		}else{
	        			$(target).attr("disabled","disabled");	
	        		}		
	        	}
	        },
	        btn2: function () {
	                  layer.msg("您取消了操作!");
	              },
	        cancel: function () {
	                  layer.msg("您取消了操作!");
	              }
	        });
    }
    
    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/21 22:05:32
     * @Brief: Asynchronous ajax request.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] url  确定后要请求的接口
     * @Param[in] method  请求接口的方法, 如get, post
     * @Param[in] data  请求接口的post数据
     * @Param[in] checkResult  校验响应数据的回调函数, 带一个result参数
     * @Prerequisite: 
     * @Return: 
     */
    function ajaxMatching(target, url, method, data, checkResult) {
    	$(target).blur();
        if (!method) {
            method = "get";
        }
    	
        $.ajax({ 
            url: url, 
            data: {"data":data},  
            type: method,  
            cache: false,  
            dataType: 'json',  
            success: function(result) {
                if(result.state === "true" ){
                    // No check or check return true.
                	$(target).val(result.mg);
                	if(result.mg!="配资失败"){
                		$(target).attr("disabled","disabled");
                		layer.confirm("配资正在进行中，请您20分钟后再查看状态 !", {
                    	      btn: ['OK'] //按钮
                    	    }, function(){
                    	    	location.reload();
                    	});
                	}
                	//location.reload();
                 }else{
                	layer.msg("请求失败!");
                 }
            },
            error: function() {  
            	   layer.msg("请求失败!"); 
            }  
        });
    }    

    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/21 22:05:32
     * @Brief: Asynchronous ajax request.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] url  确定后要请求的接口
     * @Param[in] method  请求接口的方法, 如get, post
     * @Param[in] data  请求接口的post数据
     * @Param[in] checkResult  校验响应数据的回调函数, 带一个result参数
     * @Prerequisite: 
     * @Return: 
     */
    function ajaxRequest(target, url, method, data, checkResult) {
    	$(target).blur();
        if (!method) {
            method = "get";
        }
        var index = layer.load(1, {
            shade: layerConf.shadeRate,
            shadeClose: false
        });

        $.ajax({ 
            url: url, 
            data: {"data":data},  
            type: method,  
            cache: false,  
            dataType: 'json',  
            success: function(result) {
                //layer.close(index);
                if(result.state === "true" ){
                    // No check or check return true.
                    layer.confirm(result.msg, {
              	      btn: ['OK'] //按钮
              	    }, function(){
              	    	layer.close(index);
              	    	location.reload();
              	    });
                 }else{
                	layer.msg(result.msg);
                	layer.close(index);
                	var interval = setInterval(function () {
                		clearInterval(interval);
                		location.reload();
                	}, 3000);
                 }
                 $(target).attr("disabled",false);
            },
            error: function() {  
                       layer.close(index);
                       layer.msg("请求失败!");
                   }  
        });
    }
    
    /**
     * @Author: robin
     * @Date: 2016/07/21 21:54:01
     * @Brief: 取消按钮操作.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] url 指定链接
     * @Prerequisite: 
     * @Return: 
     */  
    function gotoBack(target,url){
    	if(url != null && url != ""){
    		self.location.href = url;
    	}else{
    		self.location=document.referrer;
    	}
    }     

    /**
     * @Author: zhaoyuankui
     * @Date: 2016/07/22 21:20:34
     * @Brief: Pop up a text area.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  标题
     * @Param[in] url  确定后要请求的接口
     * @Param[in] method  请求接口的方法, 如get, post
     * @Prerequisite: 
     * @Return: 
     */
    function popUpTextArea(target, title, url, method) {
    	var dates = true;
    	$(target).blur();
        var styles = "style='width:100%; height:100%;'";
        layer.prompt({
            title: title, 
            type: 1,
            shade: layerConf.shadeRate,
            area: ['60%', '50%'],
            shadeClose: false,
            content: "<br><form style='padding:10px 20px;height:70%;' id='return-form' method='" + method + "' action='" + url + "' target=\"textalrs\"><textarea onkeyup=\"repaceNoms(this);\"" + styles + " id='reason' name='reason'/><iframe name=\"textalrs\" frameborder=\"0\" width=\"1\" height=\"1\" src=\"\"></iframe></form>",
            btn: ['确认退回', '取消'],
            yes: function () {
            	if($("#reason").val().replace(/\s/gi,'')!=""){
            	   if($("#reason").val().length<=400){	
	            		$("#return-form").submit(function () {
	            			$.ajax({
	            				type: 'POST', 
	            				data: $(this).serialize(), 
	            				url: url,
	            	            success: function () {
	                              location.reload();//重新加载页面
	            	            },
	            	            error: function (xhr) {
	            	                //...
	            	            } 
	            	        });
	            			return false;
	            		});
	            		if(dates) {
	                        dates = false;
	            			$("#return-form").submit();
	            		}
//	                   $("#return-form").addAttr("disabled","true");
                   }else{
                	   layer.msg("最多只能输入400字!");
                   }   
                }else{
                   layer.msg("提交内容为空，无法完成操作!");	
                }
            },
            btn2: function () {
                      layer.msg("您取消了操作!");
                  },
            cancel: function () {
                        layer.msg("您取消了操作!");
                    }
        });
    }
    
    /**
     * @Author: robin
     * @Date: 2016/07/22 21:20:34
     * @Brief: 生成自定义模板式弹框.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  标题
     * @Param[in] templetUrl  获取弹框内容的链接
     * @Param[in] formid  异步请求值的表单ID
     * @Prerequisite: 
     * @Return: 
     */
    function popUpInput(target, title, templetUrl, formid) {   	
    	var templet = "";
    	$(target).blur();
        $.ajax({ 
            url: templetUrl, 
            data: {"data":formid},  
            type: "get",  
            cache: false,  
            dataType: 'text',  
            success: function(result) {
                templet = result;
                layer.prompt({
                    title: title, 
                    type: 1,
                    shade: layerConf.shadeRate,
                    area: ['500px', '500px'],
                    shadeClose: false,
                    content: "<br>"+result,
                    btn: ['确认', '取消'],
                    yes: function () {
                    	//alert(formid);
                        $("#"+formid).submit();
                    	//$('form').submit();
                        alert("表单提交成功!");
                        location.reload();
                    },
                    btn2: function () {
                              alert("您取消了操作!");
                          },
                    cancel: function () {
                    		 alert("您取消了操作!");
                            }
                });
            },
            error: function() {  
                   }  
        });   
    }
    
    function popUpInputChangeBankInfo(target, title, templetUrl, formid) {   
    	$(target).blur();
    	var templet = "";
    	$(target).attr("disabled", true);
    	if($("input[name='bankNumber']").val()=="" || $("input[name='bankBranch']").val()=="" || $("input[name='bankCity']").val()==""){
    		alert("银行账户信息不完整，请检查！");
    		return false;
    	}
        $.ajax({ 
            url: templetUrl, 
            data: {"data":formid},  
            type: "get",  
            cache: false,  
            dataType: 'text',  
            success: function(result) {
                templet = result;
                layer.prompt({
                    title: title, 
                    type: 1,
                    shade: layerConf.shadeRate,
                    area: ['500px', '300px'],
                    shadeClose: false,
                    content: "<br>"+result,
                    btn: ['确认', '取消'],
                    yes: function () {
                        $("#"+formid).submit();
                        //var os = $("#modifUserBank").contents().find("body").html();
                        setTimeout(waitOneSecond, 1000);
                    	$(target).attr("disabled", false);
                    },
                    btn2: function () {
                              alert("您取消了操作!");
                          	$(target).attr("disabled", false);
                          },
                    cancel: function () {
                    		 alert("您取消了操作!");
                    	     $(target).attr("disabled", false);
                            }
                });
            },
            error: function() {  
                   }  
        });   
    }
    
	function waitOneSecond(){
    	location.reload();
    }
    
    /**
     * 满标列表----放款专用事件，确认放款时走回调函数，等待宝付返回数据，并给用户提示
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  标题
     * @Param[in] templetUrl  获取弹框内容的链接
     * @Param[in] formid  异步请求值的表单ID
     */
    function popUpInput2(target, title, templetUrl, formid) { 
    	$(target).blur();
    	var templet = "";
    	var layerIdx = 0;
    	var loanId;
    	var dates = true;//确认放款按钮不可连点校验
    	
    	/**
    	 * 用户确认放款后 的回调函数
    	 */
    	var confirmLend = function () {
        	var url = "/sxsVault/fullScaleLoan/lending.do?data=" + loanId;
        	var loadingLayerIdx = 0;
        	$("#" + formid).submit(function () {
    			$.ajax({
    				type: 'POST', 
    				data: $(this).serialize(), 
    				url: url,
					success : function(data) {
						layer.close(loadingLayerIdx);
						var resp = JSON.parse(data);//获取回调的json数据
						alert(resp.msg);//从json数据中获取msg信息，并弹出给用户展示
						
						location.reload();// 重新加载页面
					},
    	            error: function (xhr) {
    	            	layer.close(loadingLayerIdx);
    	            	alert("服务请求失败！");
    	            }
    	        });
    			
    			// 关闭窗口
    			layer.close(layerIdx);
    			boxShow = false;
    			// 显示loading动画
    			loadingLayerIdx = layer.open({
    				title: false,
    				content: '<div style="font-size:20px;">正在放款中，请耐心等待！</div>',
    				shadeClose: false,
                    shade: layerConf.shadeRate,
                    closeBtn: false,
                    btn: false
    			});	
    			return false;
    		});	 
        	
        	if(dates) {
                dates = false;
                $("#"+formid).submit();
        	}  
        	
    	};
    	
    	/**
    	 * 弹出放款窗口
    	 */
    	var popLendWindow = function (result) {
            templet = result;
            loanId = GetQueryStringByUrl(templetUrl, "data");
            layerIdx = layer.prompt({
                title: title, 
                type: 1,
                shade: layerConf.shadeRate,
                area: ['500px', '500px'],
                shadeClose: false,
                content: result,
                btn: ['确认放款', '取消'],
                yes: confirmLend,
                btn2: function () {
                          alert("您取消了操作!");
                          boxShow = false;
                      },
                cancel: function () {
                		 alert("您取消了操作!");
                		 boxShow = false;
                        }
            }); 
            boxShow = true;
    	};
    	if(!boxShow){
	        $.ajax({ 
	            url: templetUrl, 
	            data: {"data":formid},  
	            type: "get",  
	            cache: false,  
	            dataType: 'text',  
	            success: popLendWindow,
	            error: function() {  
	                   }  
	        }); 
        }
    }
       
    
    /**
     * @Author: robin
     * @Date: 2016/07/22 21:20:34
     * @Brief: 生成自定义模板式弹框.
     * @Param[in] target  触发事件的元素, 通常传this
     * @Param[in] title  标题
     * @Param[in] templetUrl  获取弹框内容的链接
     * @Param[in] formid  异步请求值的表单ID
     * @Prerequisite: 
     * @Return: 
     */
    function ajaxGetVault(target, getVaultURL, data) {   	
    	var templet = "";
    	if(getVaultURL.indexOf("?") > 0 ){
    		getVaultURL = getVaultURL + "&data="+data;
    	}else{
    		getVaultURL = getVaultURL + "?data="+data;
    	}
        $.ajax({ 
            url: getVaultURL, 
            data: {},  
            type: "get",  
            cache: false,  
            dataType: 'json',  
            success: function(result) {
            	if(result.state === "true" ){
            	   $("#"+target).html(result.data+"&nbsp; ");
            	}else{
            	   $("#"+target).html("——");	
            	}
            },
            error: function() {  
                   }  
        });  
    } 
    
    function ajaxGetVaultForAccout(target, getVaultURL, data) {   	
    	var templet = "";
        $.ajax({ 
            url: getVaultURL, 
            data: {},  
            type: "get",  
            cache: false,  
            dataType: 'text',  
            success: function(result) {
            	if( data == "edit" ){
            	   $("#"+target).html(result.replace(/input/g,"span"));
            	}else{
            	   $("#"+target).html(result);	
            	}
            },
            error: function() {  
                   }  
        });  
    }     
    
    function popUpBankWindow(target, title, url, data,bankSelect_input,isentrust_input,id) {
    	var templet = "";
    	$(target).blur();
    	url = url+"&bankSelect_input="+bankSelect_input+"&isentrust_input="+isentrust_input+"&bid="+id+"&val="+$(target).val();
                layer.prompt({
                    title: title, 
                    type: 1,
                    shade: layerConf.shadeRate,
                    area: ['500px', '500px'],
                    shadeClose: false,
                    content: '<iframe id="bankView" frameborder="0" height="400px" width="450px" scrolling="no" name="bankView" src="'+url+'"></iframe>',
                    btn: ['确认', '取消'],
                    yes: function () {
                    	$("#bankView").contents().find("#loan_bank").submit();
                        location.reload();
                    },
                    btn2: function () {
                              alert("您取消了操作!");
                          },
                    cancel: function () {
                    		 alert("您取消了操作!");
                            }
                });
  
    } 
    
    function popUpPuDownWindow(target, title, templetUrl, formid) {
    	var templet = "";
    	$(target).blur();
    	//url = url+"&bankSelect_input="+bankSelect_input+"&isentrust_input="+isentrust_input+"&bid="+id+"&val="+$(target).val();
                layer.prompt({
                    title: title, 
                    type: 1,
                    shade: layerConf.shadeRate,
                    area: ['500px', '500px'],
                    shadeClose: false,
                    content: '<iframe id="bankView" frameborder="0" height="400px" width="450px" scrolling="no" name="bankView" src="'+templetUrl+'"></iframe>',
                    btn: ['确认', '取消'],
                    yes: function () {
                    	$("#bankView").contents().find("#"+formid).submit();
                        location.reload();
                    },
                    btn2: function () {
                              alert("您取消了操作!");
                          },
                    cancel: function () {
                    		 alert("您取消了操作!");
                            }
                });
  
    } 

    return {
        butClick: butClick,
        popUpWindow: popUpWindow,
        popUpMinWindow:popUpMinWindow,
        popUpConfirm: popUpConfirm,
        ajaxRequest: ajaxRequest,
        checkBoxAll: checkBoxAll,
        popChoose: popChoose,
        gotoBack: gotoBack,
        popUpTextArea: popUpTextArea,
        popUpInput: popUpInput,
        popUpInputChangeBankInfo: popUpInputChangeBankInfo,
        popUpInput2: popUpInput2,
        ajaxGetVault: ajaxGetVault,
        ajaxMatching:ajaxMatching,
        popUpMatchingConfirm:popUpMatchingConfirm,
        popUpBankWindow:popUpBankWindow,
        popUpPuDownWindow:popUpPuDownWindow,
        ajaxGetVaultForAccout:ajaxGetVaultForAccout
    };
}) (top);
/* vim: set ts=4 sw=4 sts=4 tw=100 */
